<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="手机运营商" prop="mobileCarrier">
        <el-input
          v-model="queryParams.mobileCarrier"
          placeholder="请输入手机运营商"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司标识" prop="companyLogo">
        <el-input
          v-model="queryParams.companyLogo"
          placeholder="请输入公司标识"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否巡游出租车：1-是 0-否" prop="isTaxiDriver">
        <el-select
          v-model="queryParams.isTaxiDriver"
          placeholder="请选择是否巡游出租车：1-是 0-否"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OR)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['ride:extend:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['ride:extend:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['ride:extend:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="关联 id" align="center" prop="driverId" />
      <el-table-column label="手机运营商" align="center" prop="mobileCarrier" />
      <el-table-column label="公司标识" align="center" prop="companyLogo" />
      <el-table-column label="注册地行政区代码" align="center" prop="districtCode" />
      <el-table-column
        label="驾驶员出生日期"
        align="center"
        prop="birthDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="国籍" align="center" prop="nationality" />
      <el-table-column label="驾驶员民族" align="center" prop="ethnic" />
      <el-table-column label="驾驶证编号" align="center" prop="driverLicenseNo" />
      <el-table-column label="婚姻" align="center" prop="maritalStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MARRAY" :value="scope.row.maritalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="外语能力" align="center" prop="foreignLang" />
      <el-table-column label="使用 APP 版本号" align="center" prop="appVersion" />
      <el-table-column label="使用地图类型" align="center" prop="mapType" />
      <el-table-column label="驾驶员学历" align="center" prop="education">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.STUDENT" :value="scope.row.education" />
        </template>
      </el-table-column>
      <el-table-column label="户口登记机关名称" align="center" prop="householdReg" />
      <el-table-column label="户口住址" align="center" prop="householdAddr" />
      <el-table-column label="现居住地址" align="center" prop="currentAddr" />
      <el-table-column label="驾驶员照片文件编号" align="center" prop="driverPhotoNo" />
      <el-table-column label="驾驶证扫描件文件编号" align="center" prop="licenseScanNo" />
      <el-table-column label="准驾车型" align="center" prop="carType" />
      <el-table-column
        label="初次领证日期"
        align="center"
        prop="licenseFirstDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="驾驶证有效期限起"
        align="center"
        prop="licenseValidStart"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="驾驶证有效期限止"
        align="center"
        prop="licenseValidEnd"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="是否巡游出租车" align="center" prop="isTaxiDriver">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OR" :value="scope.row.isTaxiDriver" />
        </template>
      </el-table-column>
      <el-table-column label="网约车驾驶员资格证号" align="center" prop="taxiCertNo" />
      <el-table-column label="资格证发证机构" align="center" prop="taxiCertOrg" />
      <el-table-column
        label="资格证发证日期"
        align="center"
        prop="taxiCertDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="初次领取资格证日期"
        align="center"
        prop="taxiCertFirstDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="资格证有效起"
        align="center"
        prop="taxiCertValidStart"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="资格证有效止"
        align="center"
        prop="taxiCertValidEnd"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="报备日期"
        align="center"
        prop="reportDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="是否专职" align="center" prop="isFullTime">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OR" :value="scope.row.isFullTime" />
        </template>
      </el-table-column>
      <el-table-column label="是否在黑名单" align="center" prop="inBlacklist">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OR" :value="scope.row.inBlacklist" />
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="serviceType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TYPE" :value="scope.row.serviceType" />
        </template>
      </el-table-column>
      <el-table-column label="合同签署公司" align="center" prop="contractCompany" />
      <el-table-column label="紧急联系人" align="center" prop="emergencyContact" />
      <el-table-column label="紧急联系人电话" align="center" prop="emergencyPhone" />
      <el-table-column label="紧急联系人通信地址" align="center" prop="emergencyAddr" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['ride:extend:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['ride:extend:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ExtendForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ExtendApi, Extend } from '@/api/ride/extend'
import ExtendForm from './ExtendForm.vue'

/** 司机扩展 列表 */
defineOptions({ name: 'Extend' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<Extend[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  driverId: undefined,
  mobileCarrier: undefined,
  companyLogo: undefined,
  districtCode: undefined,
  birthDate: [],
  nationality: undefined,
  ethnic: undefined,
  driverLicenseNo: undefined,
  maritalStatus: undefined,
  foreignLang: undefined,
  appVersion: undefined,
  mapType: undefined,
  education: undefined,
  householdReg: undefined,
  householdAddr: undefined,
  currentAddr: undefined,
  driverPhotoNo: undefined,
  licenseScanNo: undefined,
  carType: undefined,
  licenseFirstDate: [],
  licenseValidStart: undefined,
  licenseValidEnd: undefined,
  isTaxiDriver: undefined,
  taxiCertNo: undefined,
  taxiCertOrg: undefined,
  taxiCertDate: [],
  taxiCertFirstDate: [],
  taxiCertValidStart: undefined,
  taxiCertValidEnd: undefined,
  reportDate: [],
  isFullTime: [],
  inBlacklist: undefined,
  serviceType: undefined,
  contractCompany: undefined,
  emergencyContact: undefined,
  emergencyPhone: undefined,
  emergencyAddr: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ExtendApi.getExtendPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ExtendApi.deleteExtend(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除司机扩展 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await ExtendApi.deleteExtendList(checkedIds.value)
    checkedIds.value = []
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: Extend[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ExtendApi.exportExtend(queryParams)
    download.excel(data, '司机扩展.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
